﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class PosController : Controller
    {
        //
        // GET: /Pos/
        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dt in context.DSChucVu
                            join pb in context.DSPhongBan on dt.IDPhongBan equals pb.ID
                            // where dt.IDCanBo == id
                            select new
                            {
                                dt.ID,
                                dt.Ten,
                                dt.IDPhuCap,
                                dt.IDPhongBan,
                                dt.IsPhuCap,
                                pb.TenPhongBan
                                
                            } into abc
                            orderby abc.ID ascending
                            select abc).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.TenPhongBan,
                                 customer.Ten
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index()
        {
            List<ChucVu> DSTG = PosModels.GetDSChucVu();
            ViewBag.DSChucVu = DSTG;
            return View();
        }
        //hien thi giao dien them dantoc
        public ActionResult Add()
        {
            List<PhuCap> dsPhuCap = PhuCapModels.GetDSPhuCap();
            ViewBag.DSPhuCap = dsPhuCap;
            List<tblNhomChucVu> dsNhomChucVu = NhomChucVuModels.GetDanhSachNhomChucVu();
            ViewBag.DSNhomChucVu = dsNhomChucVu;
            return View();
        }
        //them dan toc
        [HttpPost]
        public ActionResult Add(FormCollection fm)
        {
            
            //int stt = Convert.ToInt32(fm["STT"]);
            string ten = fm["Ten"];
            ChucVu dt = new ChucVu();
            //gan vao bang
            
            dt.Ten= ten;
            dt.IDPhuCap = Convert.ToInt32(fm["IDPhuCap"]);
            dt.IDPhongBan = Convert.ToInt32(fm["ListPhongBan"]);
            dt.IDNhomChucVu = Convert.ToInt32(fm["IDNhomChucVu"]);
            //dt.Active = 0;
            PosModels.Add(dt);

            return RedirectToAction("Index");
        }
        //xoa dan toc

        public ActionResult Delete(int id)
        {

            PosModels.Delete(id);
            return RedirectToAction("Index");
        }

        //sua dan toc
        public ActionResult Update(int id)
        {
            List<PhuCap> dsPhuCap = PhuCapModels.GetDSPhuCap();
            ViewBag.DSPhuCap = dsPhuCap;
            List<tblNhomChucVu> dsNhomChucVu = NhomChucVuModels.GetDanhSachNhomChucVu();
            ViewBag.DSNhomChucVu = dsNhomChucVu;
            ChucVu listDT = PosModels.GetChiTietDSChucVu(id);
            ViewBag.DSChucVu = listDT;
            PhongBan pb = PhongBanModels.GetChiTietPhongBan((int)listDT.IDPhongBan);
            ViewBag.phongban = pb;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection fm, int id)
        {

            //int stt = Convert.ToInt32(fm["STT"]);
            string ten= fm["Ten"];
            ChucVu dt = PosModels.GetChiTietDSChucVu(id);
            //dt.STT = stt;
            dt.Ten = ten;
            dt.IDPhuCap = Convert.ToInt32(fm["IDPhuCap"]);
            dt.IDPhongBan = Convert.ToInt32(fm["ListPhongBan"]);
            dt.IDNhomChucVu = Convert.ToInt32(fm["IDNhomChucVu"]);
            PosModels.Edit(dt);
            return RedirectToAction("Index");
        }
        public int GetConfirmDelete(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from cb in context.DSCanBo
                        where cb.ChucVuHienTai == id
                        select cb;
            return query.ToList().Count;
        }
    }
}
